//
//  AliyunPVPlayShareView.m
//  HYQPlayer-Ali
//
//  Created by zhuxingjian on 2018/7/16.
//  Copyright © 2018年 ZXJ. All rights reserved.
//

#import "AliyunPVPlayShareView.h"
#import "UIView+AliyunLayout.h"
#import "AliyunPVShareButton.h"
#import "AliyunPVUtil.h"

static const int ALPV_SHARE_BTN_HEIGHT = 75;
static const int ALPV_SHARE_BTN_WIDTH = 65;
static const int ALPV_SHARE_BTN_SPACE_LR = 35; //左右间距
static const int ALPV_SHARE_BTN_SPACE_TB = 30; //上下间距
static const int ALPV_SHARE_BTN_BASE_TAG = 10000;


@interface AliyunPVPlayShareView ()

@property (nonatomic, strong) UIControl *bgControl;
@property (nonatomic, strong) NSArray *sharePlaformArr;

@end

@implementation AliyunPVPlayShareView

- (UIControl *)bgControl
{
    if (!_bgControl) {
        _bgControl = [[UIControl alloc] init];
        [_bgControl addTarget:self action:@selector(controlButton:) forControlEvents:UIControlEventTouchDown];
    }
    return _bgControl;
}

- (NSArray *)sharePlaformArr
{
    return @[@{@"icon" : @"alpv_share_weixinCircular", @"title" : @"微信朋友圈"},
             @{@"icon" : @"alpv_share_weixin", @"title" : @"微信"},
             @{@"icon" : @"alpv_share_sina", @"title" : @"新浪微博"},
             @{@"icon" : @"alpv_share_QQ", @"title" : @"QQ"},
             @{@"icon" : @"alpv_share_QQspace", @"title" : @"QQ空间"}
             ];
}

#pragma mark - 便利初始化函数
- (instancetype)init{
    return [self initWithFrame:CGRectZero];
}

#pragma mark - 指定初始化函数
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame: frame]) {
        [self addSubview:self.bgControl];
    }
    return self;
}

-(void)layoutSubviews{
    self.bgControl.frame = CGRectMake(0, 0, self.aliyun_width, self.aliyun_height);
    CGFloat top  = (self.aliyun_height - (ALPV_SHARE_BTN_HEIGHT * 2 + ALPV_SHARE_BTN_SPACE_TB)) / 2;
    CGFloat leftMargin = (self.aliyun_width - (ALPV_SHARE_BTN_SPACE_LR * 2 + ALPV_SHARE_BTN_WIDTH * 3)) / 2;
    for (int i = 0; i < self.sharePlaformArr.count; i++) {
        AliyunPVShareButton *tempButton = [self viewWithTag:ALPV_SHARE_BTN_BASE_TAG + i];
        CGFloat tempY = top + (i / 3) * (ALPV_SHARE_BTN_HEIGHT + ALPV_SHARE_BTN_SPACE_TB);
        
        if (tempButton) {
            tempButton.frame = CGRectMake(leftMargin + i % 3 * (ALPV_SHARE_BTN_WIDTH + ALPV_SHARE_BTN_SPACE_LR), tempY, ALPV_SHARE_BTN_WIDTH, ALPV_SHARE_BTN_HEIGHT);
            continue;
        }
        AliyunPVShareButton *btn = [[AliyunPVShareButton alloc] initWithFrame:CGRectMake(leftMargin + i % 3 * (ALPV_SHARE_BTN_WIDTH + ALPV_SHARE_BTN_SPACE_LR), tempY, ALPV_SHARE_BTN_WIDTH, ALPV_SHARE_BTN_HEIGHT)];
        btn.iconImageView.backgroundColor = [UIColor whiteColor];
        btn.tag = ALPV_SHARE_BTN_BASE_TAG + i;
        [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        NSDictionary *info = self.sharePlaformArr[i];
        btn.iconImageView.image = [AliyunPVUtil imageWithNameInBundle:info[@"icon"]];
        btn.textLabel.text = info[@"title"];
        [self addSubview:btn];
    }
}

- (void)controlButton:(UIControl *)sender
{
    [UIView animateWithDuration:0.3 animations:^{
        if ([AliyunPVUtil isInterfaceOrientationPortrait]) {
            CGRect frame = self.frame;
            frame.origin.x = self.aliyun_width;
            self.frame = frame;
        }else{
            CGRect frame = self.frame;
            frame.origin.x = SCREEN_WIDTH;
            self.frame = frame;
        }
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)buttonClicked:(AliyunPVShareButton *)sender
{
    [self controlButton:nil];
    if ([self.shareViewDelegate respondsToSelector:@selector(AliyunPVPlayShareView:shareType:)]) {
        [self.shareViewDelegate AliyunPVPlayShareView:self shareType:sender.tag - ALPV_SHARE_BTN_BASE_TAG];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
